package top.kaoshanji.q1ch06e;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import top.kaoshanji.q1ch06e.aspects.LoggingAspect;
import top.kaoshanji.q1ch06e.aspects.SecurityAspect;
import top.kaoshanji.q1ch06e.config.ProjectConfig;
import top.kaoshanji.q1ch06e.model.Comment;
import top.kaoshanji.q1ch06e.services.CommentService;

import java.util.logging.Logger;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/4/29 14:53
 */
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {ProjectConfig.class})
public class AppTests {

    private Logger serviceLogger;
    private Logger loggingAspectLogger;
    private Logger securityAspectLogger;

    @Autowired
    private LoggingAspect loggingAspect;

    @Autowired
    private SecurityAspect securityAspect;

    @Autowired
    private CommentService commentService;

    @BeforeEach
    public void before() {
        this.loggingAspectLogger = Mockito.mock(Logger.class);
        loggingAspect.setLogger(loggingAspectLogger);

        this.securityAspectLogger = Mockito.mock(Logger.class);
        securityAspect.setLogger(securityAspectLogger);

        this.serviceLogger = Mockito.mock(Logger.class);
        commentService.setLogger(serviceLogger);
    }

    /**
     * 代码清单test-q1ch06e-01：测试一下切面执行链
     */
    @Test
    public void testAspectInterceptsPublishCommentMethod() {
        Comment comment = new Comment();
        comment.setText("Test comment text");
        comment.setAuthor("Test comment author");

        commentService.publishComment(comment);

        Mockito.verify(serviceLogger).info("发布Comment：" + comment.getText());
        Mockito.verify(securityAspectLogger).info("...安全切面拦截了目标方法的调用...");
        Mockito.verify(loggingAspectLogger).info("...日志切面拦截了目标方法的调用...");

    }



}
